package cn.edu.xhu.library_management.user.service.impl;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import cn.edu.xhu.library_management.user.client.BookClient;
import cn.edu.xhu.library_management.user.entity.LmUserOhter;
import cn.edu.xhu.library_management.user.entity.LmUserStudent;
import cn.edu.xhu.library_management.user.entity.LmUserTeacher;
import cn.edu.xhu.library_management.user.entity.dto.User;
import cn.edu.xhu.library_management.user.mapper.LmUserOhterMapper;
import cn.edu.xhu.library_management.user.mapper.LmUserStudentMapper;
import cn.edu.xhu.library_management.user.mapper.LmUserTeacherMapper;
import cn.edu.xhu.library_management.user.service.LmUserOhterService;
import cn.edu.xhu.library_management.user.service.LmUserStudentService;
import cn.edu.xhu.library_management.user.service.LmUserTeacherService;
import cn.edu.xhu.library_management.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: MMJ
 * @Date: 2023 - 04 - 22
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private LmUserOhterMapper otherMapper;
    @Resource
    private LmUserStudentMapper studentMapper;
    @Resource
    private LmUserTeacherMapper teacherMapper;
    @Resource
    private BookClient bookClient;
    
    @Autowired
    private BCryptPasswordEncoder encoder;

    @Override
    public User login(User user) {
        int accountLen = user.getAccount().length();
        User u = new User();
        // 用户可借阅总数
        int borrowNum = 0;
        switch (accountLen){
            case 11: {
                // 校外用户
                LmUserOhter userOther = otherMapper.selectById(user.getAccount());
                if (!encoder.matches(user.getPassword(),userOther.getOthPwd())){
                    // 账号密码错误
                    return null;
                }
                // 设置用户信息
                u.setAccount(userOther.getOthAccount());
                u.setImgUrl(userOther.getOthImg());
                u.setNickName(user.getNickName());
                u.setBalance(userOther.getOthBalance());
                borrowNum = userOther.getOthBorrowNum();
                
            };break;
            case 9: {
                // 教师
                LmUserTeacher userTeacher = teacherMapper.selectById(user.getAccount());
                if (!encoder.matches(user.getPassword(),userTeacher.getThPwd())){
                    return null;
                }
                // 设置用户信息
                u.setAccount(userTeacher.getThAccount());
                u.setImgUrl(userTeacher.getThImg());
                u.setNickName(user.getNickName());
                u.setBalance(userTeacher.getThBalance());
                borrowNum = userTeacher.getThBorrowNum();

            };break;
            default:{
                if (accountLen>11 && accountLen <=20){
                    // 学生
                    LmUserStudent userStudent = studentMapper.selectById(user.getAccount());
                    if (!encoder.matches(user.getPassword(),userStudent.getStPwd())){
                        return null;
                    }
                    // 设置用户信息
                    u.setAccount(userStudent.getStAccount());
                    u.setImgUrl(userStudent.getStImg());
                    u.setNickName(user.getNickName());
                    u.setBalance(userStudent.getStBalance());
                    borrowNum = userStudent.getStBorrowNum();
                }else {
                    throw new LibraryManagementException(ResultCode.ERROR,"账号不存在");
                }
            }
        }
        // 设置已经借阅数
        int size = bookClient.noReBorrow(user.getAccount()).size();
        u.setNowBorrowNum(size);
        // 剩余可借阅数
        u.setRemainBorrowNum(borrowNum - size);
        // 设置图片路径
        String imgUrl = u.getImgUrl();
        u.setImgUrl("/file"+imgUrl.substring(imgUrl.lastIndexOf("/")));
        return u;
    }
}
